package demo.hdz.mediacodecdecode.widget;

import android.view.MotionEvent;
import android.view.View;

/**
 * @author hdz
 * @date 2022/06/25 19:46
 */
public class ViewPressEffect implements View.OnTouchListener {
    public ViewPressEffect() {

    }

    public ViewPressEffect(View view) {
        view.setOnTouchListener(this);
    }

    public View addView(View view) {
        view.setOnTouchListener(this);
        return view;
    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                view.setAlpha(0.5f);
                break;
            case MotionEvent.ACTION_CANCEL:
                view.setAlpha(1.0f);
                break;
            case MotionEvent.ACTION_UP:
                view.setAlpha(1.0f);
                long interval = (event.getEventTime() - event.getDownTime());
                if (interval > 1000) {
                    if (!view.performLongClick()) {
                        view.performClick();
                    }
                } else {
                    view.performClick();
                }
                break;
            default:
                return false;
        }
        return true;
    }
}
